package spring.cloud.study.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {

	private static final Logger Logger = LoggerFactory.getLogger(MovieController.class);
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@GetMapping("/ribbon/getByRest")
	public String getByRest() {
		return restTemplate.getForObject("http://EUREKA-PROVIDER/provider/hello", String.class);
	}

	@GetMapping("/ribbon/getByClient")
	public String getServiceInstance() {
		ServiceInstance si = this.loadBalancerClient.choose("EUREKA-PROVIDER");
		Logger.info("{}:{}:{}", si.getServiceId(), si.getHost(), si.getPort());
		return "消费者调用成功";
	}

}
